//
//  TSAccountTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/12/15.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSAccountTableViewCellDelegate : NSObjectProtocol {
    func didClickItem(idx:Int)
}

class TSAccountCellItem: UIView {
    
    convenience init(_ imgName:String, title:String) {
        self.init()
        self.configViews(imgName: imgName, title: title)
    }
    
    func configViews(imgName:String, title:String)  {
        
        let icon = UIImageView.init(image: UIImage(named: imgName))
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerX.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 25, height: 25))
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = title
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#363636")
        titleLabel.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon.snp.bottom).offset(10)
        }
    }
}

class TSAccountTableViewCell: UITableViewCell {

    var containerView : UIView!
    weak var delegate : TSAccountTableViewCellDelegate? = nil
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func tapGesture(gesture:UITapGestureRecognizer) {
        
        let tag = (gesture.view?.tag)!
        if self.delegate != nil {
            self.delegate?.didClickItem(idx: tag)
        }
    }
    
    func setupCell(array:Array<TSNormalCellModel>) {
        
        var itemArray : Array<TSAccountCellItem> = []
        
//        for model in array {
//            let item = TSAccountCellItem.init(model.imgName, title: model.title)
//            item.tag =
//            contentView.addSubview(item)
//            itemArray.append(item)
//        }
        
        for idx in 0..<array.count {
            let model = array[idx]
            let item = TSAccountCellItem.init(model.imgName, title: model.title)
            item.tag = idx
            item.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapGesture(gesture:))))
            contentView.addSubview(item)
            itemArray.append(item)
        }
        
        itemArray.snp.distributeSudokuViews(fixedItemWidth: 52, fixedItemHeight: 48, warpCount: 4, edgeInset: UIEdgeInsets(top: 20, left: 15, bottom: 0, right: 15))
    }
}
